package com.idopartx.phonelightning.ui

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import api.ttfullvideo.FullVideo_API_TT
import com.idopartx.phonelightning.widget.LoadingDialog
import com.dotools.toutiaolibrary.TT_FullVideo
import com.dotools.umlibrary.UMPostUtils
import com.dotools.utils.ChannelMgr
import com.idopartx.phonelightning.application.CLApplication
import com.idopartx.phonelightning.common.Constant

class FullVideoActivity : AppCompatActivity() {
    private var tt: TT_FullVideo? = null
    var loadingDialog: LoadingDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadingDialog = LoadingDialog(this, "视频正在加载中,请稍等...")
        loadingDialog?.show()
        showInteraction()
        CLApplication.isFullVideoIng = true
    }

    //显示全屏广告
    private fun showInteraction() {
        tt = TT_FullVideo()

        val posId = if (ChannelMgr.getUmengChannel(this).equals("huawei")) {
            UMPostUtils.onEvent(this, "halfscreen_chaping_show")
            "949015464"
        } else {
            UMPostUtils.onEvent(this, "fullscreen_chaping_show")
            "949015485"
        }

        tt?.LoadTTFullVideo(this, Constant.TT_APPID, posId, 1, 1, false, 1, object :
            FullVideo_API_TT.TTFullVideoListener {
            override fun onObShow() {
                loadingDialog?.dismiss()
                UMPostUtils.onEvent(applicationContext, "fullscreen_chaping_show")
            }

            override fun onObVideoBarClick() {
                UMPostUtils.onEvent(applicationContext, "fullscreen_chaping_click")
            }

            override fun onObClose() {
                finish()
            }

            override fun onVideoComplete() {

            }

            override fun onSkippedVideo() {
                UMPostUtils.onEvent(applicationContext, "fullscreen_chaping_skip")
            }

            override fun onError(code: Int, message: String) {
                loadingDialog?.dismiss()
                UMPostUtils.onEvent(applicationContext, "fullscreen_chaping_pullfailed")
                Log.e("FullVideoError", "$message--$code")
                finish()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        UMPostUtils.onActivityResume(this@FullVideoActivity)
    }

    override fun onPause() {
        super.onPause()
        UMPostUtils.onActivityPause(this@FullVideoActivity)
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, 0)
    }

    override fun onDestroy() {
        CLApplication.isFullVideoIng = false
        super.onDestroy()
        tt?.onDestroy()
    }
}